/*
 * 文件名:VehicleServiceImpl.java
 * 创建时间:2010-10-01
 * 版本:2.0
 * 版权所有:杨涛 walle1027@gmail.com
 */
package com.yangtao.wms.master.vehicle.service.impl;

import com.yangtao.framework.hibernate.Selector;
import com.yangtao.framework.service.BaseService;
import com.yangtao.framework.util.StringHelper;
import com.yangtao.wms.dao.VehicleDao;
import com.yangtao.wms.dao.VehicleDetailDao;
import com.yangtao.wms.domain.Vehicle;
import com.yangtao.wms.domain.VehicleDetail;
import com.yangtao.wms.master.vehicle.service.VehicleService;
import com.yangtao.wms.master.vehicle.web.VehicleModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 代码生成器 v-2.0
 * @version 2.0
 */
@Service()
public class VehicleServiceImpl extends BaseService implements VehicleService {

	@Autowired
	protected VehicleDao vehicleDao;
	@Autowired
	protected VehicleDetailDao vehicleDetailDao;

	/**
	 * 查询记录，并且分页
	 *
	 * @param model 页面参数
	 */
	@Override
	public VehicleModel list(VehicleModel model) {
		Selector selector = new Selector();
		selector.add(".vehicleDetail");
		vehicleDao.findPageByCondition(selector, model.getPage(), model.getCondition());
		return model;
	}

	/**
	 * 取得唯一记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public VehicleModel loadVehicle(VehicleModel model) {
		if (StringHelper.isNotEmpty(model.getVehicle().getId())) {
			Selector selector = new Selector();
			selector.add(".vehicleDetail");
			model.setVehicle(vehicleDao.loadForReview(model.getVehicle().getId(), selector));
		}
		return model;
	}

	/**
	 * 新增记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public VehicleModel addVehicle(VehicleModel model) {
		//VO
		Vehicle vehicleVO = model.getVehicle();
		//PO
		Vehicle vehiclePO = new Vehicle();
		mapper.map(vehicleVO, vehiclePO);
		//构造关联从对象
		//构造关联对象
		VehicleDetail vehicleDetailVO = vehicleVO.getVehicleDetail();
		VehicleDetail vehicleDetailPO = new VehicleDetail();
		mapper.map(vehicleDetailVO, vehicleDetailPO);
		//设置额外属性
		setVehicleDetailProperties(vehicleDetailPO, model);
		//构造关联对象关联关系
		vehicleDetailPO.setVehicle(vehiclePO);
		vehiclePO.setVehicleDetail(vehicleDetailPO);
		//保存对象
		vehicleDao.save(vehiclePO);
		//增加保存成功的消息
		model.addInfo("add.success");
		return model;
	}

	/**
	 * 更新记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public VehicleModel updateVehicle(VehicleModel model) {
		//VO
		Vehicle vehicleVO = model.getVehicle();
		//主键
		String vehicleId = vehicleVO.getId();
		//PO
		Vehicle vehiclePO = vehicleDao.load(vehicleId);
		//复制页面属性
		mapper.map(vehicleVO, vehiclePO);
		//TODO 增加其他属性
		//TODO 更新关联对象
		//构造关联从对象
		//构造关联对象，一对一的情况下，假定它不会更新关联关系
		String vehicleDetailId = vehicleVO.getVehicleDetail() == null ? null : vehicleVO.getVehicleDetail().getId();
		VehicleDetail vehicleDetailPO = vehicleDetailDao.load(vehicleDetailId);
		if (vehicleDetailPO != null) {
			//根据页面展示项 copy字段，此处要留有扩展点
			mapper.map(vehicleVO.getVehicleDetail(), vehicleDetailPO);
			//增加其他属性
			setVehicleDetailProperties(vehicleDetailPO, model);
			//构造关联对象关联关系
			vehicleDetailPO.setVehicle(vehiclePO);
		}

		//增加保存成功的消息
		model.addInfo("update.success");
		return model;
	}

	/**
	 * 删除记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public VehicleModel deleteVehicle(VehicleModel model) {
		vehicleDao.delete(model.getVehicle().getId());
		model.addInfo("delete.success");
		return model;
	}

	/**
	 * 设置关联对象:车辆详细信息表 的其他属性
	 * 页面上无法传递过来的属性可以在此增加
	 * 子类重写此方法可以在新增和修改的时候为关联实体增加额外的属性
	 *
	 * @param vehicleDetail 关联对象
	 * @param model         页面参数类
	 */
	protected void setVehicleDetailProperties(final VehicleDetail vehicleDetail, final VehicleModel model) {
	}
}
